package com.example.easyutils.musicpage;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.MainActivity;
import com.example.easyutils.R;

public class MusicNotification extends BaseActivity {
    Context context;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_notification);
        context = MusicNotification.this;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "chat";//根据业务执行
            String channelName = "聊天消息";//这个是channelid 的解释，在安装的时候会展示给用户看
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
            channelId = "subscribe";
            channelName = "订阅消息";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);
        }
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void sendChatMsg(View view) {

        Intent intent = new Intent(MusicNotification.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MusicNotification.this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Log.d("text______________67",BitmapFactory.decodeResource(getResources(), R.drawable.leaf)+"");
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = null;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             notification = new NotificationCompat.Builder(this, "chat")
                    .setContentTitle("收到一条聊天消息")
                    .setContentText("今天中午吃什么？")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.history)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.leaf))
                    .setAutoCancel(true)
//                .setCustomContentView(customView)//自定义
                    .setContentIntent(pendingIntent)//点击跳转activity
                    .build();
        }else{
            notification = new Notification.Builder(this)
                    .setSmallIcon(R.drawable.history)
                    .setWhen(System.currentTimeMillis())
                    .setContentTitle("title")
                    .setContentText("content")
                    .setContentIntent(pendingIntent)
                    .build();
        }

        manager.notify(101, notification);
    }

    public void sendSubscribeMsg(View view) {
        RemoteViews customView = new RemoteViews(MusicNotification.this.getPackageName(), R.layout.music_layout);//自定义通知
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到一条订阅消息")
                .setContentText("地铁沿线30万商铺抢购中！")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.rubbish_danger)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.leaf))
                .setAutoCancel(true)
                .setCustomContentView(customView)//自定义
                .build();
        manager.notify(202, notification);
    }
}
